<template>
  <div class="register">
    <h2>milink后台管理系统</h2>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>请注册</span>
      </div>
      <el-form
        :model="registerUser"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="registerUser.username"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="registerUser.email"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="registerUser.password"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="password2">
          <el-input type="password" v-model="registerUser.password2"></el-input>
        </el-form-item>
        <el-form-item label="选择身份" prop="identity">
          <el-select
            v-model="registerUser.identity"
            placeholder="请选择您的身份"
          >
            <el-option label="管理员" value="manager"></el-option>
            <el-option label="普通员工" value="employee"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >注册</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
let Datestr = "";
export default {
  data() {
    var validatePass2 = (rule, value, callback) => {
      if (value !== this.registerUser.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      registerUser: {
        username: "",
        identity: "",
        password: "",
        password2: "",
        email: "",
        date: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名称", trigger: "blur" },
          {
            min: 2,
            max: 30,
            message: "长度在 2 到 30 个字符",
            trigger: "blur",
          },
        ],
        email: [
          {
            type: "email",
            required: true,
            message: "邮箱格式不正确",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 30,
            message: "长度在 6 到 30 个字符",
            trigger: "blur",
          },
        ],
        identity: [
          {
            required: true,
            message: "请至少选择一个身份",
            trigger: "change",
          },
        ],
        password2: [
          { required: true, message: "确认密码不能为空", trigger: "blur" },
          {
            min: 6,
            max: 30,
            message: "长度在 6 到 30 个字符",
            trigger: "blur",
          },
          {
            validator: validatePass2,
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios
            .post("api/user/register", this.registerUser)
            .then((result) => {
              console.log(result);
              if (result.data.code == 1) {
                //注册成功
                this.$message({
                  message: `${result.data.msg}`,
                  type: "success",
                });
                this.$router.push("/login");
              } else {
                this.$message.error(`注册失败:${result.data.msg}`);
              }
            })
            .catch((err) => {});
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
  mounted() {
    let data = new Date();
    Datestr = `${data.getFullYear()}-${data.getMonth() + 1}-${data.getDate()}`;
    this._data.registerUser.date = Datestr;
  },
};
</script>

<style scoped>
.register {
  width: 100%;
  height: 100vh;
  background-image: linear-gradient(120deg, #89f7fe 0%, #66a6ff 100%);
}

.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both;
}

.register h2 {
  text-align: center;
  font-size: 18px;
  transform: translateY(180px);
}

.box-card {
  margin: 0 auto;
  transform: translateY(210px);
  width: 480px;
}
</style>